package com.vgmobile.cucrever2.home.category;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.vgmobile.cucrever2.R;
import com.vgmobile.cucrever2.ResideMenuActivity;
import com.vgmobile.cucrever2.api.ItemListHoz;
import com.vgmobile.cucrever2.global.BaseBackPressedListener;
import com.vgmobile.cucrever2.home.EventAdapter;
import com.vgmobile.cucrever2.home.ItemEvent;
import com.vgmobile.cucrever2.home.category.child.CategoryChildFragment;
import com.viewpagerindicator.CirclePageIndicator;

public class CategoryParentFragment extends Fragment{
	private String cateID;
	private final int ACTION_NEXT_EVENT = 1;

	private ResideMenuActivity activity;
	private View view;
	private ListView listView;
	private ListCategoryAdapter listCategoryAdapter;
	private EventAdapter eventAdapter;
	private ViewPager eventViewPager;
	private CirclePageIndicator catEventIndicator;
	private boolean isRunning;
	private Handler mHandler;
	
	@SuppressLint("HandlerLeak")
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if(view == null){
			mHandler = new Handler() {
				public void handleMessage(android.os.Message msg) {
					switch (msg.what) {
					case ACTION_NEXT_EVENT:
						int pos = (eventViewPager.getCurrentItem() + 1) % eventAdapter.getCount();
						eventViewPager.setCurrentItem(pos);
						break;
					}
				};
			};
			activity = (ResideMenuActivity) getActivity();
			view = inflater.inflate(R.layout.category_parent_layout, container, false);
			listView = (ListView) view.findViewById(R.id.category_parent_list);
			eventViewPager = (ViewPager) view.findViewById(R.id.category_parent_event_viewpager);
			loadEventPage();
		}else{
			((ViewGroup)view.getParent()).removeView(view);
		}
		
		ArrayList<ItemEvent> listItemEvent = new ArrayList<ItemEvent>();
        for (int i = 0; i < 5; i++) {
        	ItemEvent item = new ItemEvent();
			if (i%2 == 0) {
				item.setImageLink("https://dl.dropboxusercontent.com/u/71228229/TestCucre/event_1.png");
			}else{
				item.setImageLink("https://dl.dropboxusercontent.com/u/71228229/TestCucre/event_2.png");
			}
			item.setEventName("World Class " + i);
			listItemEvent.add(item);
		}
        eventAdapter = new EventAdapter(activity, listItemEvent);
        
        ArrayList<ItemListHoz> listItemCat = new ArrayList<ItemListHoz>();
        for (int i = 0; i < 12; i++) {
        	ItemListHoz item = new ItemListHoz();
        	item.setTitle("Category thứ  " + i);
        	listItemCat.add(item);
		}
        listCategoryAdapter = new ListCategoryAdapter(activity, listItemCat);
        
        listView.setAdapter(listCategoryAdapter);
		eventViewPager.setAdapter(eventAdapter);
		eventViewPager.setOffscreenPageLimit(0);
		catEventIndicator.setViewPager(eventViewPager);
		catEventIndicator.setCurrentItem(0);
		setListViewHeightBasedOnChildren(listView);
        activity.setTitleMenuHome("Danh mục sản phẩm");
        
		activity.setOnBackPressedListener(new BaseBackPressedListener(getFragmentManager()));
		isRunning = true;
		return view;
	}
    
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapterView, View view, int postion, long id) {
				Fragment newFragment = new CategoryChildFragment();
				activity.nextFragment(newFragment, null);
			}
		});
	}
	
	
	public void loadEventPage() {
		catEventIndicator = (CirclePageIndicator) view.findViewById(R.id.category_parent_event_indicator);
		catEventIndicator.setFillColor(getResources().getColor(R.color.circle_indicator_fill_color));
		catEventIndicator.setPageColor(getResources().getColor(R.color.circle_indicator_page_color));
		catEventIndicator.setStrokeWidth(0);

		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(5 * 1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				while (isRunning) {
					try {
						Message msg = new Message();
						msg.what = ACTION_NEXT_EVENT;
						mHandler.sendMessage(msg);
						Thread.sleep(5 * 1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		t.start();
	}
	
	private void setListViewHeightBasedOnChildren(ListView listView) {
	    ListAdapter listAdapter = listView.getAdapter();
	    if (listAdapter == null) {
	        return;
	    }
	    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
	    int totalHeight = 0;
	    View view = null;
	    for (int i = 0; i < listAdapter.getCount(); i++) {
	        view = listAdapter.getView(i, view, listView);
	        if (i == 0) {
	            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
	        }
	        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
	        totalHeight += view.getMeasuredHeight();
	    }
	    ViewGroup.LayoutParams params = listView.getLayoutParams();
	    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
	    listView.setLayoutParams(params);
	    listView.requestLayout();
	}
	
	@Override
	public void onDestroyView() {
		isRunning = false;
		super.onDestroyView();
	}



}
